#include "dialogabout.h"
#include "tempgraph.h"
#include "ui_dialogabout.h"

DialogAbout::DialogAbout(QWidget *parent, GlobalData * globals) :
    QDialog(parent),
    ui(new Ui::DialogAbout)
{
    ui->setupUi(this);

    this->globals = globals;

    this->setWindowTitle("About");
    ui->labelTitle->setText("tempgraph");
    ui->labelDescription->setText("This is intended as a tool for educational or research purposes, " \
                                  "and enables you to generate graphs showing changes in average temperatures " \
                                  "over time, using data from the Global Historical Climatology Network. " \
                                  "The creation of this program was inspired by curiosity about temperature " \
                                  "data arising from the \"Climategate\" scandal - particularly the blog posts " \
                                  "of Eric Raymond. I wanted to cut out the media circus and armchair " \
                                  "commentators and see what the data looks like for myself, so that I could " \
                                  "cultivate some informed opinion on the topic of global warming.");
    ui->labelCredits->setText("Written by Bob Mottram <bob@sluggish.dyndns.org>");
    ui->labelVersion->setText(getVersion());
}

DialogAbout::~DialogAbout()
{
    delete ui;
}

QString DialogAbout::getVersion()
{
    QString command, result = "";
    QString version_filename = globals->dataDirectory + QString(PATH_SEPARATOR) + "version.txt";
    FILE * fp;
    char linestr[256];

    command = "tempgraph --version > " + version_filename;
    if (system(command.toStdString().c_str())==0) {
        fp = fopen(version_filename.toStdString().c_str(),"r");
        if (fp) {
            while (!feof(fp)) {
                if (fgets(linestr , 255 , fp) != NULL) {
                    if (linestr != NULL) {
                        result += QString(linestr);
                    }
                }
            }
            fclose(fp);
        }
    }
    return result;
}
